package screens;

import media.AudioManager;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.me.jnrgame.JnR;

public class OptionsScreen implements Screen {

	private JnR game;
	private Stage stage;
	private Slider musicSlider;
	private Slider soundSlider;
	private CheckBox muteBox;
	private float musicVolumeBuffer;
	private float soundVolumeBuffer;

	/*
	 * ID that specifies which screen is showed when the back button is pressed
	 * ID 0: MainMenuScreen ID 1: LevelScreen
	 */

	private int parentID;
	Skin skin;

	public OptionsScreen(JnR _game) {
		super();
		game = _game;
		game.setOptionsScreen(this);
		stage = new Stage(600, 360, true);
		// game.getCamera().viewportWidth = stage.getWidth();
		// game.getCamera().viewportHeight = stage.getHeight();
		// game.getCamera().update();
		TextureAtlas menuAtlas = new TextureAtlas(
				Gdx.files
						.internal("data/screen-layouts/optionsmenu/uiskin.atlas"));
		Gdx.input.setInputProcessor(stage);
		parentID = 0;
		skin = new Skin(
				Gdx.files
						.internal("data/screen-layouts/optionsmenu/uiskin.json"),
				menuAtlas);
		Table table = new Table(skin);
		table.setWidth(stage.getWidth());
		table.setHeight(stage.getHeight());
		table.add("Options").spaceBottom(10).align(Align.left);
		table.row();
		table.add("Music:").spaceBottom(10);
		table.row();
		musicSlider = new Slider(0f, 100f, 2f, false, skin);
		musicSlider.setValue(AudioManager.getMusicVolume() * 100);
		musicVolumeBuffer = musicSlider.getValue();
		musicSlider.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				AudioManager.setMusicVolume(musicSlider.getValue() / 100.0f);
			}

		});
		table.add(musicSlider).spaceBottom(20f);
		table.row();
		table.add("Sounds:").spaceBottom(10);
		table.row();
		soundSlider = new Slider(0f, 100f, 2f, false, skin);
		soundSlider.setValue(AudioManager.getSoundVolume() * 100);
		soundVolumeBuffer = soundSlider.getValue();
		soundSlider.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				AudioManager.setSoundVolume(soundSlider.getValue() / 100.0f);
			}

		});
		table.add(soundSlider).spaceBottom(20f);
		table.row();
		muteBox = new CheckBox("   Mute Volume", skin);
		muteBox.setSize(60, 60);

		// register the button "options"
		muteBox.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				if (muteBox.isChecked()) {
					musicVolumeBuffer = musicSlider.getValue();
					soundVolumeBuffer = soundSlider.getValue();
					musicSlider.setValue(0f);
					soundSlider.setValue(0f);
					System.out.println("Volume box checked: "
							+ musicSlider.getValue());

				} else {
					musicSlider.setValue(musicVolumeBuffer);
					soundSlider.setValue(soundVolumeBuffer);

					System.out.println("Volume box unchecked: "
							+ musicSlider.getValue());

				}
			}

		});
		table.add(muteBox).align(Align.center).spaceBottom(30);
		table.row();

		TextButton backButton = new TextButton("Back", skin);
		backButton.addListener(new ChangeListener() {

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				// TODO Auto-generated method stub
				updateOptionsScreen();

				if (parentID == 0) {
					game.setScreen(game.getMainMenuScreen());
					Gdx.input.setInputProcessor(game.getMainMenuScreen()
							.getStage());

				} else {
					if (parentID == 1) {
//						 game.pauseGame(false);
						game.getLevelScreen().addHUDButtons();
						
						
						game.setScreen(game.getLevelScreen());
						Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
						Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
						
						game.getLevelScreen().getWorldAssitant()
								.renderBackground();
						game.addPauseMenu();
						Gdx.input.setInputProcessor(game.getLevelScreen()
								.getStage());
					}
				}
				game.resetViewPort();
			}

		});
		table.add(backButton).size(140f, 40f).uniform().spaceBottom(10)
				.align(Align.center);
		stage.addActor(table);
	}

	@Override
	public void render(float delta) {
		if (parentID == 1) {
			game.getLevelScreen().removeHUDButtons();
		}
		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		System.out.println("cam " + game.getCamera().viewportWidth + " "
				+ game.getCamera().viewportHeight);
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		stage.draw();
		// Table.drawDebug(stage);
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	public void setParentID(int ID) {

		parentID = ID;

	}

	public Stage getStage() {

		return stage;
	}

	public void updateOptionsScreen() {

		game.setOptionsScreen(this);
	}
}
